﻿Imports System.Activities
Imports System.ComponentModel
Imports System.Activities.Presentation.PropertyEditing
Imports System.Collections.ObjectModel
Imports System.Activities.Presentation.Metadata
Imports System.Drawing

Public Class FileExistsCheckActivity
    Inherits AsyncCodeActivity
    Implements IRegisterMetadata

    Public Property TargetFile As InArgument(Of String)
    Public Property Result As OutArgument(Of Boolean)

    Private Delegate Function asyncCheckFileDelegate(ByVal target As String) As Boolean

    Public Sub New()
        Me.DisplayName = My.Resources.FileExistsCheckDisplayName
    End Sub

    Protected Overrides Function BeginExecute(ByVal context As System.Activities.AsyncCodeActivityContext, ByVal callback As System.AsyncCallback, ByVal state As Object) As System.IAsyncResult
        Dim target = context.GetValue(Me.TargetFile)
        Dim asyncExecute = New asyncCheckFileDelegate(AddressOf CheckFile)
        context.UserState = asyncExecute

        Return asyncExecute.BeginInvoke(target, callback, state)
    End Function

    Protected Overrides Sub EndExecute(ByVal context As System.Activities.AsyncCodeActivityContext, ByVal result As System.IAsyncResult)
        Dim asyncExecute = TryCast(context.UserState, asyncCheckFileDelegate)
        Dim getResult = asyncExecute.EndInvoke(result)

        context.SetValue(Me.Result, getResult)
    End Sub

    Private Function CheckFile(ByVal target As String) As Boolean
        Dim result As Boolean = False
        Try
            result = My.Computer.FileSystem.FileExists(target)
        Catch ex As Exception

        End Try
        Return result
    End Function


    Public Sub Register() Implements System.Activities.Presentation.Metadata.IRegisterMetadata.Register
        Dim build As New AttributeTableBuilder

        'FileExistsCheck Activity
        build.AddCustomAttributes(GetType(FileExistsCheckActivity),
                                              New ToolboxBitmapAttribute(GetType(FileExistsCheckActivity), "FileExistsCheck.png"))
        build.AddCustomAttributes(GetType(FileExistsCheckActivity), "Target",
                                              New RequiredArgumentAttribute,
                                              New EditorAttribute(GetType(FilepathPropertyEditor), GetType(DialogPropertyValueEditor)),
                                              New CategoryAttribute(My.Resources.FileExistsCheckCategory),
                                              New DisplayNameAttribute(My.Resources.FileExistsCheckTargetDisplay),
                                              New DescriptionAttribute(My.Resources.FileExistsCheckTargetDescription))
        build.AddCustomAttributes(GetType(FileExistsCheckActivity), "Result",
                                              New CategoryAttribute(My.Resources.FileExistsCheckCategory),
                                              New DisplayNameAttribute(My.Resources.FileExistsCheckResultDisplay),
                                              New DescriptionAttribute(My.Resources.FileExistsCheckResultDescription))

        MetadataStore.AddAttributeTable(build.CreateTable)
    End Sub
End Class
